---
title: "Cleaning country regions"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load RCS Dataset
  regions_raw <- 
    import("../raw-data/x-country-regions/country-regions.xlsx")
```

# Clean

```{r}
# additional countries
  extras <- 
    tribble(
      ~country, ~region, ~global_region,
      "Kosovo", "Europe & North America", "Global North",
      "Czechoslovakia", "Europe & North America", "Global North",
      "German Democratic Republic", "Europe & North America", "Global North")

# clean
  regions_clean <- 
    regions_raw %>%
    filter(country != "France, Metropolitan") %>% 
    bind_rows(extras) %>% 
    mutate(regions_country_common = countryname(country)) %>% 
    mutate(region = 
             case_when(
               region == "Europe" ~ "Europe & North America",
               region == "North America" ~ "Europe & North America",
               region == "South/Latin America" ~ "Latin America",
               region == "South/Central America" ~ "Latin America",
               region == "Africa" ~ "Sub-Saharan Africa",
               
               regions_country_common %in% c("Algeria", "Bahrain", "Djibouti", "Egypt", "Iran", "Iraq", 
                                             "Israel", "Jordan", "Kuwait", "Lebanon", "Libya", "Malta", 
                                             "Morocco", "Oman", "Palestine", "Qatar", "Saudi Arabia", "Syria", 
                                             "Tunisia", "United Arab Emirates", "Yemen", "Mauritania") ~ "Middle East & North Africa",
               
               regions_country_common %in% c("Somalia", "Sudan") ~ "Sub-Saharan Africa",
               
               TRUE ~ region)) %>% 
    arrange(regions_country_common) %>% 
    select(-country) %>% 
    filter(!is.na(regions_country_common)) %>% 
    arrange(region, regions_country_common)
```

# Save data

```{r}
  saveRDS(regions_clean, "../cleaned-data/x-2-country-regions.rds")
```
